// src/store/Modules.ts

export default {
    auth: {
        namespaced: true,
        state: {
            username: "",
            userId: "",
            token: "",
            isLogin:false,
            uToast:null,
        },
        mutations: {
            setToken(_state: any, token: string) {
                if (token) {
                    _state.token = token
                } else {
                    _state.token = ""
                }
            },
            setUsername(_state: any, username: string) {
                if (username) {
                    _state.username = username
                } else {
                    _state.username = ""
                }
            },
            setUserId(_state: any, userId: string) {
                if (userId) {
                    _state.userId = userId
                } else {
                    _state.userId = ""
                }
            },
            setIsLogin(_state: any, isLogin: boolean) {
                _state.isLogin = isLogin
            },
            setUToast(_state: any, uToast: any) {
                _state.uToast = uToast
            },
        },
        actions: {
            setToken(context: any, token: string) {
                context.commit('setToken', token)
            },
            setUsername(context: any, username: string) {
                context.commit('setUsername', username)
            },
            setUserId(context: any, userId: string) {
                context.commit('setUserId', userId)
            },
            setIsLogin(context: any, isLogin: boolean) {
                context.commit('setIsLogin', isLogin)
            },
            setUToast(context: any, uToast:any) {
                context.commit('setUToast', uToast)
            },
            logout(context: any) {
                context.commit('setToken')
                context.commit('setUsername')
                context.commit('setUserId')
                context.commit('setIsLogin',false)
                uni.navigateTo({
                    url: '/pages/login/index',
                    success: () => {
                        uni.showToast({
                            title: '你已被登出，请重新登录',
                            icon: 'none'
                        })
                    }
                })
            },
        },
        getters: {
            getToken(_state: any, _getters: any): string {
                return _state.token
            },
            getUsername(_state: any, _getters: any): string {
                return _state.username
            },
            getUserId(_state: any, _getters: any): string {
                return _state.userId
            },
            getIsLogin(_state: any, _getters: any): string {
                return _state.isLogin
            },
            getUToast(_state: any, _getters: any): string {
                return _state.uToast
            }
        }
    }
}